package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import jsyf.oa.dao.system.MessageReceiverDao;
import jsyf.oa.entity.system.MessageReceiverEntity;
import jsyf.oa.facade.system.MessageReceiverFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Maqiuqiu on 2018/7/21.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class MessageReceiverService implements MessageReceiverFacade{

    @Autowired
    private MessageReceiverDao messageReceiverDao;

    @Override
    public String save(String jsonData) {
        MessageReceiverEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), MessageReceiverEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = messageReceiverDao.add(entity);
        } else {
            MessageReceiverEntity messageReceiverEntity = (MessageReceiverEntity) messageReceiverDao.getIBaseEntityById(entity.getId());
            messageReceiverEntity.setMessageId(entity.getMessageId());
            messageReceiverEntity.setUserId(entity.getUserId());
            messageReceiverEntity.setUserType(entity.getUserType());
            messageReceiverEntity.setReadFlag(entity.getReadFlag());
            rows = messageReceiverDao.update(messageReceiverEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public Boolean delById(String id) {
        return messageReceiverDao.delById(id) > 0;
    }

}
